package com.example.onlinemusicserver2024.oss;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

import java.io.ByteArrayInputStream;
import java.io.File;

public class OssTest {

    public static void main(String[] args) throws Exception {
        OssTest test = new OssTest();
        test.getAllFile();
    }


    // 上传文件
    public void upload() {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("oss-cn-guangzhou.aliyuncs.com", "LTAI5tGK2j8XwbSwkYozgNJd", "QqGSkRfjMooecjBZcz2qDTuAyiV9Je");
        try {
            // 填写Byte数组。
            byte[] content = "Hello OSS, 你好世界".getBytes();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest("online-music-2024", "test-1.txt", new ByteArrayInputStream(content));

            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            System.out.println("上传成功");
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 上传文件
     */
    public void uploadFile(){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("oss-cn-guangzhou.aliyuncs.com", "LTAI5tGK2j8XwbSwkYozgNJd", "QqGSkRfjMooecjBZcz2qDTuAyiV9Je");

        String filePath= "D:\\students_list.xlsx";

        File file = new File(filePath);

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest("online-music-2024", file.getName(), file);

        // 创建PutObject请求。
        PutObjectResult result = ossClient.putObject(putObjectRequest);

        System.out.println("上传文件成功");

    }

    public void getAllFile(){

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("oss-cn-guangzhou.aliyuncs.com", "LTAI5tGK2j8XwbSwkYozgNJd", "QqGSkRfjMooecjBZcz2qDTuAyiV9Je");


        // 构造ListObjectsRequest请求。
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest("online-music-2024");
        // 设置prefix参数来获取fun目录下的所有文件。
       // listObjectsRequest.setPrefix("fun/");

        // 递归列举fun目录下的所有文件。
        ObjectListing listing = ossClient.listObjects(listObjectsRequest);

        // 遍历所有文件。
        System.out.println("Objects:");
        for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) {
            System.out.println(objectSummary.getKey());
        }

    }
}
